<?php
namespace app\admin\controller;
use think\Controller;
use caretree\Caretree;
use think\console\output\descriptor\Console;
use think\Loader;
class Article extends Controller 
{
    public function lst()
    {
	//join方法默认是inner join  若要设置 需在第三个参数指定 alias 别名 连接查询需要指定字段
		$articleRes = db('article')->field('a.*,c.cate_name')->alias('a')->join('cate c','a.cate_id=c.id')->order('id DESC')->paginate(10);
		$this->assign([
			'artRes'=>$articleRes
		]);
		return view('list');
	}
	public function add()
	{
		$cate = new Caretree();
		if(request()->isPost()){
			$data = input('post.');
			$data['addtime']=time();
			if($data['link_url'] && stripos($data['link_url'],'http://') === false){
				$data['link_url']='http://'.$data['link_url'];
			}
			

			if($_FILES['thumb']['tmp_name']){
				$data['thumb']=$this->upload();
			}
			
			$validate = validate('article');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			$add= db('article')->insert($data);
			if($add){
				$this->success('添加文章成功!','lst');
			}else{
				$this->error('添加文章失败');
			}
			return;
		}
		$cateRes = db('cate')->order('sort ASC')->select();
		$cateRes = $cate->Caretree($cateRes);
		$this->assign([
			'cateRes' => $cateRes
		]);
		return view();
	}
	public function edit()
	{
		$cate = new Caretree();
		if(request()->isPost()){
			$data = input('post.');
			if($data['link_url'] && stripos($data['link_url'],'http://') === false){
				$data['link_url']='http://'.$data['link_url'];
			}
			
			//文件上传输出全部提交
			//dump($_FILES);  //获取包含图片的信息 
			//dump($data);die; // 不能获取上传图片的信息
			//处理上传图片
			if($_FILES['thumb']['tmp_name']){
				$oldarticles = db('article')->field('thumb')->find($data['id']);
				//拼接图片的绝对路径D:\phpstudy_pro\WWW\shop\tp5\public\
				$oldarticleImg = IMG_UPLOADS.DS.$oldarticles['thumb'];
				// echo $oldarticleImg."<br/>";
				// echo IMG_UPLOADS;
				// die;
				if(file_exists($oldarticleImg)){
					//删除指定文件的路径
					@unlink($oldarticleImg);
				}
				
				$data['thumb']=$this->upload();
			}
			// //验证
			// $validate = validate('article');
    		// if(!$validate->check($data)){
			//     $this->error($validate->getError());
			// }
			$save= db('article')->update($data);
			
			if($save !== false){
				$this->success('修改文章成功!','lst');
			}else{
				$this->error('修改文章失败');
			}
			return;
		}
		$id= input('id');
		$article = db('article')->find($id);
		$cateRes = db('cate')->order('sort ASC')->select();
		$cateRes = $cate->Caretree($cateRes);
		$this->assign([
			'arts' => $article,
			'cateRes'=> $cateRes
		]);
		return view();
	}
	public function del($id)
	{
		$article = db('article');
		$arts = $article->field('thumb')->find($id);
		$thumbSrc = IMG_UPLOADS.DS.$arts['thumb'];
		if(file_exists($thumbSrc)){
			@unlink($thumbSrc);
		}
		$del = db('article')->delete($id);
		if($del){
			$this->success('删除文章成功!','lst');
		}else{
			$this->error('删除文章失败');
		}
		return view();
	}
	public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('thumb');
	    
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' .DS.'static'. DS . 'uploads');
	        if($info){
	            return $info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
				die;
	        }
	    }
	}
	//ueditor图片管理
	public function imglist(){
		// echo UEDITOR;
		// echo '<br>'.APP_PATH;
		$_files = my_scandir();
		// dump($files);
		$files = array();
		foreach ($_files as $k =>$v){
			if(is_array($v)){
				foreach($v as $k1=>$v1){
					//用空数组接受 若该数组已存在值这下标为最后一个加1
					$v1 = str_replace(UEDITOR,HTTP_UEDITOR,$v1);
					$files[] =$v1;
				}
			}else{
				$v = str_replace(UEDITOR,HTTP_UEDITOR,$v);
				$files[]=$v;
			}
		}
		// dump($files);
		// die;
		$this->assign([
			'imgRes' => $files,
		]);
		return view();
	}

	//删除图片ajax
	//对应的是   data : {imgsrc:imgsrc},  json数据格式的key
	public function delimg(){
		$imgsrc=input('imgsrc'); //使用原先的请求路劲不能删除图片 自定义图片路径为DEL_UEDITOR
		$imgsrc=DEL_UEDITOR.$imgsrc;
		if(file_exists($imgsrc)){
			if(@unlink($imgsrc)){
				echo 1;
			}else{
				echo 2;
			}
		}else {
			echo 3;
		}
	}

	
}
